<template>
	<view class="">
		<swiper indicator-dots autoplay circular>
			<swiper-item v-for="item in swipers" :key="item.id">
				<image :src="item.img"></image>
			</swiper-item>
		</swiper>
		<view>
			<u-form :model="form" ref="uForm" :label-style="{'width':'100rpx'}">
				<u-form-item left-icon="account" label="学号" prop="account">
					<u-input v-model="form.account" />
				</u-form-item>
				<u-form-item left-icon="lock" label="密码" prop="password">
					<u-input type="password" v-model="form.password" />
				</u-form-item>
			</u-form>
			<u-button style="width: 50%;" @click="formSubmit">登录</u-button>
		</view>
	</view>

</template>

<script>
	//import base64 from '../../util/base64.js';
	let base64 = require('../../util/base64.js').Base64;
	export default {
		data() {
			return {
				swipers: [], //存储轮播图片
				form: {
					account: "",
					password: "",
				},
				rules: {
					account: [{
						required: true,
						message: '请输入学号！',
						trigger: 'blur',
					}],
					password: [{
						required: true,
						message: '请输入密码！',
						trigger: 'blur'
					}]
				}
				// account: 18034520231,
				// pwd: 'd2p6OTkxMDE2'
			}
		},
		onLoad() {
			this.$myRequest({
				url: '/api/getlunbo'
			}).then(res => {
				this.swipers = res.data.message
			})
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			// 提交登录信息
			formSubmit(e) {
				console.log(e)
				this.$refs.uForm.validate(valid => {
					if (valid) {
						var header = {
							'content-type': 'application/x-www-form-urlencoded',
							'X-Requested-With': 'XMLHttpRequest',
							'Cookie': uni.getStorageSync("sessionid"),
						}
						const res = this.$schoolRequest({
							url: '/login!doLogin.action',
							method: 'POST',
							data: {
								account: this.form.account,
								pwd: base64.encode(this.form.password)
							},
							header: header,
							success: (res) => {
								if (res && res.header && res.header['Set-Cookie']) {
									uni.setStorageSync("sessionid", res.header['Set-Cookie'])
								}
								console.log(res.data)
								if (res.data.status == 'y') {
									uni.showToast({
										title: '登录成功',
										icon: 'success',
										success: function() {
											wx.switchTab({
												url: '/pages/index/index',
											})
										}
									})
								} else if (res.data.status == 'n') {
									uni.showToast({
										title: '用户名或者密码错误，请重新输入',
										icon: 'none'
									})
								}
							},
							fail: (err) => {
								console.log(err)
							}
						})
					} else {
						return;
					}
				});
				// let account = e.detail.value.account;
				// let pwd = base64.encode(e.detail.value.pwd);
			}
		},
	}
</script>

<style lang="scss">
	swiper {
		width: 750rpx;
		height: 350rpx;

		image {
			height: 100%;
			width: 100%;
		}
	}

	/*顶部*/
	.top {
		height: 400rpx;
		width: 100%;
	}

	.u-input {
		margin-left: 10rpx;
		border: 1px solid black;
	}
</style>
